/**************************************************************************
*   Copyright (C) 2009 Chernov Dmitry                                     *
*   diman4ik.chernov@gmail.com                                            *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "korners_engine_thread.h"
#include "korners_game_engine_ai.h"
#include "korners_game_engine_dual.h"
#include "korners_game_engine_network.h"
#include "korners_game_engine_timer.h"
#include "korners_scene.h"

#include <KDebug>


KornersEngineThread::KornersEngineThread( KornersScene* sc) : QThread(sc)
{
    curGameEngine = 0;
    sType = none;
    m_scene = sc;
}

KornersEngineThread::~KornersEngineThread()
{
    kDebug() << "deletion";
    
    if( curGameEngine != 0 )
        curGameEngine->stop();
    quit();
    wait();
    
    if( curGameEngine != 0 )
    {
        delete curGameEngine;
        curGameEngine = 0;
    }
}

void KornersEngineThread::run()
{
    if( curGameEngine != 0 )
    {
        kDebug() << "deleting cur game engine";
        delete curGameEngine;
    }

    switch(sType)
    {
        case ai:
            kDebug() << "creating game engine ai";
            curGameEngine = new KornersGameEngineAI();
            break;
        case timer:
            qDebug() << "creating game engine timer";
            curGameEngine = new KornersGameEngineTimer();
            break;
	case dual:
            kDebug() << "creating game engine local multiplayer";
            curGameEngine = new KornersGameEngineDual();
	    break;
        case network_clnt:
            kDebug() << "creating game engine network client";
            curGameEngine = new KornersGameEngineNetwork(   startParams[0].toString(),
                                                            false,
                                                            startParams[1].toInt(), 
                                                            startParams[2].toString() );
            break;
        case network_serv:
            kDebug() << "creating game engine network server";
            curGameEngine = new KornersGameEngineNetwork(   startParams[0].toString(),
                                                            true,
                                                            startParams[1].toInt() );
            break;
        default:
            break;
    }

    if(curGameEngine != 0)
    {
        connect( m_scene, SIGNAL( playerMadeMove( QPoint , QPoint ) ), 
                 curGameEngine, SLOT( playersMove( QPoint , QPoint ) ) );

        connect( m_scene, SIGNAL( capSelected(QPoint) ),
                 curGameEngine, SLOT( capSelected(QPoint) ) );


        connect( curGameEngine, SIGNAL( move(KornersMove) ),  
                 m_scene, SLOT( showMovePath(KornersMove) ) );

        connect( curGameEngine, SIGNAL( availableMoves( const QVector<QPoint>* ) ), 
                 m_scene, SLOT( showPaths( const QVector<QPoint>* ) ) );
        
        connect( curGameEngine, SIGNAL( engineMessage(QString) ), 
                 m_scene, SLOT( showEngineMess(QString) ) );

        connect( curGameEngine, SIGNAL( canPlay() ), m_scene, SLOT( canPlay() ) );

        connect( curGameEngine, SIGNAL( youWin() ), m_scene, SLOT( youWin() ) );
        connect( curGameEngine, SIGNAL( youLost() ), m_scene, SLOT( youLost() ) );

        if(( sType == network_clnt ) || ( sType == network_serv ))
        {
            connect( curGameEngine, SIGNAL( opponentName( QString ) ), 
                     m_scene, SLOT( opponentName(QString) ) );
        }
        
        if( sType == timer )
        {
            connect( m_scene, SIGNAL( startTiming() ), curGameEngine, SLOT( startTiming() ) );
            connect( curGameEngine, SIGNAL( timeChanged(QTime) ), m_scene, SLOT( newTime(QTime) ) );
        }
    }

    exec();
}

void KornersEngineThread::startGameEngineAI()
{
    sType = ai;
    restart();
}

void KornersEngineThread::startGameEngineTimer()
{
    sType = timer;
    restart();
}

void KornersEngineThread::startGameEngineDual()
{
    sType = dual;
    restart();
}

void KornersEngineThread::startGameEngineNetwork(const QString& net_name, int port, bool server, const QString& ip_adress)
{
    startParams.append(net_name);
    startParams.append(port);
    if(!server)
    {
        startParams.append(ip_adress);
        sType = network_clnt;
    }
    else
        sType = network_serv;
   
    restart();
}
